package com.rongzhe.house.ui.fragment;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;

import com.rongzhe.house.presenter.BasePresenter;
import com.rongzhe.house.ui.UiControlInterface;
import com.rongzhe.house.ui.activity.BaseActivity;

/**
 * Created by huang on 2017/11/16.
 */

public abstract class BaseFragment<T extends BasePresenter> extends Fragment
        implements UiControlInterface {
    BaseActivity mActivity;
    protected T mPresenter;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mPresenter = createPresenter();
        if (mPresenter != null) {
            mPresenter.onCreate();
        }
    }

    protected abstract T createPresenter();

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mActivity = (BaseActivity) getActivity();
    }

    @Override
    public Context getContextInternal() {
        return getActivity();
    }

    @Override
    public boolean handleNoLogin() {
        return  mActivity.handleNoLogin();
    }

    @Override
    public void launchActivityForResult(int requestCode, Intent intent) {
        mActivity.launchActivityForResult(requestCode, intent);
    }

    @Override
    public void launchActivity(boolean loginRequire, Intent intent) {
        mActivity.launchActivity(loginRequire, intent);
    }

    @Override
    public void showAlert(Dialog dialog) {
        mActivity.showAlert(dialog);
    }

    @Override
    public void dismissAlert() {
        mActivity.dismissAlert();
    }

    @Override
    public void showToast(String message) {
        mActivity.showToast(message);
    }

    @Override
    public void finish() {
        mActivity.finish();
    }

    @Override
    public void showProgress(String message) {
        mActivity.showProgress(message);
    }

    @Override
    public void dismissProgress() {
        mActivity.dismissProgress();
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        if(hidden) {
            onStop();
        } else {
            onStart();
        }
    }

    @Override
    public void onStart() {
        super.onStart();
        if (mPresenter != null) {
            mPresenter.start(getActivity().getIntent());
        }
    }

    @Override
    public void onStop() {
        super.onStop();
        if (mPresenter != null) {
            mPresenter.stop();
        }
    }
}
